
import { constantRouterMap } from '@/router'
import Layout from '../../views/layout/Layout'


var menu = []

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ commit }, route) {
      return new Promise(resolve => {
        screen(route.data)
        commit('SET_ROUTERS', menu)
        resolve()
      })
    }
  }
}

var screen = function(menus) {
  if (menus) {
    menu = []
    menus.forEach(item => {
      var parentTem = {
        path: '',
        component: Layout,
        name: '',
        meta: {
          title: '',
          icon: '',
          handId: ''
        },
        children: []
      }
      var parentpath = '/' + item.url
      parentTem.path = parentpath
      parentTem.name = item.title
      parentTem.meta = {
        title: item.title,
        icon: item.icon,
        handId: item.id
      }
      if (item.children.length) {
        var children = []
        item.children.forEach(item1 => {
        	let pathAry = item1.url.split("?")
        	let imprt_ = item1.url.split('/');
        	var importPath = imprt_[0]+'/'+imprt_[1] + '.vue'
        	var thisPath = item1.url.substring(item1.url.indexOf('/') + 1)
        	let queryStr = imprt_[2]
        
          children.push({
              path: thisPath,
              name: item1.title,
              component: () => import(`@/views/${importPath}`),
              meta: {
                title: item1.title,
                icon: item1.icon,
                handId: item1.id,
                query: queryStr,
              }
            })
        })
        parentTem.children = children
      }
      menu.push(parentTem)
    })
  }
}

export default permission
